home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Freeware / Read It Later 0.9924 / read_it_later-0.9924-fx.xpi / chrome / isreaditlater.jar / content / ISRILpr.js < prev    next >
Text File  |  2008-10-28  |  3KB  |  133 lines

  1. function ISRILpr() {
  2.     this.apiKey        =    'readitlaterlist.com';
  3.     this.apiVersion        =    1;
  4.     this.apiFormat        =    'json';
  5.     this.apiUrl        =    'http://api.postrank.com/v{VERSION}/postrank?appkey={APIKEY}&format={FORMAT}';
  6.     this.maxToSend        =    30;
  7.     
  8.     this.TOtime         =    8;
  9. }
  10.  
  11. ISRILpr.prototype = {    
  12.  
  13.     _init : function() { 
  14.         this.xul = ISRILxul;
  15.     },
  16.     
  17.     // --- //
  18.     
  19.     start : function(force) {
  20.         
  21.         if (!force && ISRILprefs.prefB('ask-aiderss')) {            
  22.             ISRILpr.xul.MsgPostRankAsk(true);
  23.             return false;
  24.         } else {
  25.             if (force && ISRILpr.xul.bip('ListMsg-pr-ask').checked) {
  26.                 ISRILprefs.setPref('ask-aiderss', false);
  27.             }
  28.             ISRILpr.xul.MsgPostRankAsk(false);
  29.         }
  30.         
  31.         if (ISRILglobals.CacheStale || !ISRILpr.cache) {
  32.         
  33.             ISRILpr.xul.ListMsgSet(true, 'Sorting');
  34.             
  35.             var s = '';
  36.             var l = ISRIL.GetList( 12, false, false );
  37.             ISRILpr.urlTable = new Array();
  38.             
  39.             for(var i in l) {
  40.                 s += '&url[]='+l[i].uri;
  41.                 ISRILpr.urlTable[l[i].uri] = l[i];
  42.                 if (i == ISRILpr.maxToSend) { break; }
  43.             }
  44.             
  45.             ISRILpr.query(s);
  46.         } else {
  47.             ISRILpr.xul.PopulateList( ISRILpr.filter( ISRILpr.cache ) );            
  48.         }
  49.         
  50.     },
  51.     
  52.     // --- //    
  53.     
  54.     query : function(urls) {
  55.         var url = ISRILpr.url();        
  56.         var ajax = new ISRILajax();
  57.         clearTimeout( ISRILpr.TO );
  58.         ISRILpr.TO = setTimeout('ISRILpr.timedOut()', ISRILpr.TOtime * 1000);
  59.         ajax.post(url, ISRILpr.callback, urls);
  60.     },
  61.     
  62.     callback : function(r) {
  63.         
  64.         try { 
  65.             var results = ISRIL.json.decode(r);
  66.             var l = new Array();
  67.             clearTimeout( ISRILpr.TO );
  68.             
  69.             var i = 0;
  70.             for(url in results) {
  71.                 try {
  72.                     l[i] = {
  73.                             itemId        : ISRILpr.urlTable[url].itemId,
  74.                             title        : ISRILpr.urlTable[url].title,
  75.                             uri        : ISRILpr.urlTable[url].uri,
  76.                             tags        : ISRILpr.urlTable[url].tags,
  77.                             postrank    : results[url].postrank,
  78.                             postrank_color    : results[url].postrank_color
  79.                     }
  80.                     i++;
  81.                 } catch(err) {}
  82.             }
  83.             
  84.             l.sort(ISRILpr.reSort);
  85.             ISRILpr.cache = l;
  86.             ISRILpr.xul.PopulateList( ISRILpr.filter(l) );
  87.         } catch(err) {
  88.             if ( ISRILpr.TO ) {
  89.                 ISRILpr.timedOut();
  90.             }
  91.         }
  92.     },
  93.     
  94.     // --- //
  95.     
  96.     filter : function(l) {
  97.         ISRIL.resetCache();
  98.         var fL = new Object();
  99.         li = 0;
  100.         for(i in l) {
  101.             if (ISRIL.ProcessItem(l[i], ISRILpr.xul.Filter.value )) {
  102.                 fL[li] = l[i];
  103.                 li++;
  104.             }
  105.         }
  106.         return fL;
  107.     },
  108.     
  109.     // --- //
  110.     
  111.     timedOut : function() {
  112.         clearTimeout( ISRILpr.TO );
  113.         ISRILpr.xul.ListErrorSet(true, {txt:ISRIL.l('PostRankConnect'), onclk: ISRILpr.start, onCancel:"ISRILxul.bip('ListSort').selectedIndex = 1;ISRILxul.FillList();"} );
  114.     },
  115.         
  116.     // --- //
  117.     
  118.     url : function(format, key, version) {
  119.         var f = (format)?format:this.apiFormat;
  120.         var k = (key)?key:this.apiKey;
  121.         var v = (version)?version:this.apiVersion;
  122.         return ISRILpr.apiUrl.replace('{VERSION}',v).replace('{APIKEY}',k).replace('{FORMAT}',f);
  123.     },
  124.     
  125.     reSort : function(a, b) {
  126.         var x = b.postrank;
  127.         var y = a.postrank;
  128.         return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  129.     },
  130.             
  131. }
  132.  
  133. var ISRILpr = new ISRILpr();